import { useEffect } from "react";

const useKeyUp = (
  targetKeyCode: number | string,
  handler: () => void
): void => {
  const keyUpHandler = (e: KeyboardEvent): void => {
    e.key === targetKeyCode && handler();
  };

  useEffect(() => {
    document.addEventListener("keyup", keyUpHandler);
    return (): void => {
      document.removeEventListener("keyup", keyUpHandler);
    };
  }, []);
};

export default useKeyUp;
